package main.java.indi.zyj.c150.dp;

public class _08_RobPlus {


    public int rob(int[] nums) {

        int len = nums.length;

        if (len == 0) return 0;
        if (len == 1) return nums[0];
        if (len == 2) return Math.max(nums[0], nums[1]);


        return Math.max(dp(nums, 0, len - 2), dp(nums, 1, len - 1));

    }

    public int dp(int[] nums, int s, int e){
        int first = nums[s], second = Math.max(nums[s], nums[s + 1]);
        for (int i = s + 2; i <= e; i++) {
            int temp = second;
            second = Math.max(second, nums[i] + first);
            first = temp;
        }
        return second;
    }

}
